from qfluentwidgets import Dialog


def check_lat_lon():
    """
    检验经纬度格式是否正常 DD MM SS
                       DDD MM SS
    :return:
    """

    def decorator(func):
        def wrapper(lat: str, lon: str):
            ls_lat = lat.strip().split()  # 纬度
            ls_lon = lon.strip().split()  # 经度
            match ls_lat:
                case ["", "", ""]:
                    Dialog("警告", "格式不正确")
                    return
                case x if x[0] > 180 or x[1] > 60 or x[2] > 60:
                    Dialog("警告", "格式不正确")
                    return
                case x if x[0] == 180 or x[1] >= 60 or x[2] >= 60:
                    Dialog("警告", "格式不正确")
                    return

            match ls_lon:
                case ["", "", ""]:
                    Dialog("警告", "格式不正确")
                    return
                case x if x[0] > 90 or x[1] > 60 or x[2] > 60:
                    Dialog("警告", "格式不正确")
                    return
                case x if x[0] == 90 or x[1] >= 1 or x[2] >= 1:
                    Dialog("警告", "格式不正确")
                    return

            func(lat, lon)

        return wrapper

    return decorator


def check_lat_lon2(func):
    """
    用于验证纬度和经度格式的装饰器（DD MM SS 或 DDD MM SS）
    :param func:
    :return:
    """

    def wrapper(lat: str, lon: str):
        def is_valid_coordinate(coord_str: str, is_lon: bool = False) -> bool:
            """
            验证经��度格式
            :param coord_str:
            :param is_lon:
            :return:
            """
            parts = coord_str.strip().split()
            if len(parts) != 3:
                return False

            try:
                degrees, minutes, seconds = map(int, parts)
                if is_lon:
                    if (
                        degrees > 180
                        or minutes > 60
                        or seconds > 60
                        or (minutes == 60 and seconds > 0)
                        or (degrees == 180 and (minutes > 0 or seconds > 0))
                    ):
                        return False
                elif (
                    degrees > 90
                    or minutes > 60
                    or seconds > 60
                    or (minutes == 60 and seconds > 0)
                    or (degrees == 90 and (minutes > 0 or seconds > 0))
                ):
                    return False
            except ValueError:
                return False

            return True

        if not (is_valid_coordinate(lat) and is_valid_coordinate(lon, True)):
            print("格式不正确")
            Dialog("警告", "格式不正确")
        else:
            func(lat, lon)

    return wrapper


# 定义修饰器模块


def check_line_edit_values(extra_param):
    def decorator(func):
        def wrapper(value1, value2):
            # 检查文本框的值
            if not value1.isdigit() or not value2.isdigit():
                print("Input must be numeric.")
                return
            # 调用原始的槽函数并传入额外参数
            func(value1 + extra_param, value2 + extra_param)

        return wrapper

    return decorator


if __name__ == '__main__':
    @check_lat_lon2
    def check_line_(lat, lon):
        print(f"Checking line{lat, lon}")

    check_line_("111", '222')
